using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TuyennvLibrary.SqlClient;
namespace ServerApi.Models
{
    public class Account
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string Oldpassword { get; set; }
        public string Name { get; set; }
        public int Status { get; set; }
        public string SyntaxSms { get; set; }
        public string Serviceid { get; set; }

        public static string COLUMN_ID = "Id";
        public static string COLUMN_USERNAME = "Username";
        public static string COLUMN_PASSWORD = "Password";
        public static string COLUMN_OLDPASSWORD = "Oldpassword";
        public static string COLUMN_NAME = "Name";
        public static string COLUMN_STATUS = "Status";
        public static string COLUMN_SYNTAX_SMS = "SyntaxSms";
        public static string COLUMN_SERVICEID = "Serviceid";

        public static string TableName = "Account";
        public Account()
        {
            this.Id = 0;
            this.Username = string.Empty;
            this.Password = string.Empty;
            this.Oldpassword = string.Empty;
            this.Name = string.Empty;
            this.Status = 0;
            this.SyntaxSms = string.Empty;
            this.Serviceid = string.Empty;
        }
        public bool HavePermissionWith(string syntax_sms)
        {
            if (!string.IsNullOrEmpty(this.SyntaxSms))
            {
                string[] arr = this.SyntaxSms.Split('|');
                if (!arr.Contains(syntax_sms))
                    return false;
            }
            return true;
        }
    }
    public class AccountModel : BaseModel<Account>
    {
        public AccountModel()
        {
            table = Account.TableName;
        }
        public bool Insert(Account obj)
        {
            try
            {
                DB.Insert(table, new SqlParameter[] {
					new SqlParameter(Account.COLUMN_USERNAME,obj.Username),
					new SqlParameter(Account.COLUMN_NAME,obj.Name),
					new SqlParameter(Account.COLUMN_STATUS,obj.Status),
                    new SqlParameter(Account.COLUMN_SYNTAX_SMS,obj.SyntaxSms),
					new SqlParameter(Account.COLUMN_SERVICEID,obj.Serviceid),
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool Update(Account obj)
        {
            try
            {
                DB.Update(table, new SqlParameter[] {
					new SqlParameter(Account.COLUMN_USERNAME,obj.Username),
					new SqlParameter(Account.COLUMN_PASSWORD,obj.Password),
					new SqlParameter(Account.COLUMN_OLDPASSWORD,obj.Oldpassword),
					new SqlParameter(Account.COLUMN_NAME,obj.Name),
					new SqlParameter(Account.COLUMN_STATUS,obj.Status),
                    new SqlParameter(Account.COLUMN_SYNTAX_SMS,obj.SyntaxSms),
					new SqlParameter(Account.COLUMN_SERVICEID,obj.Serviceid),

                }, new SqlParameter[]{
					new SqlParameter(Account.COLUMN_ID,obj.Id),
																									 
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool Delete(int Id)
        {
            try
            {
                DB.Delete(table, new SqlParameter[] { 
					new SqlParameter(Account.COLUMN_ID,Id),
																									 
                });
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool DeleteMulti(string ListID)
        {
            try
            {
                if (ListID != string.Empty)
                {
                    foreach (string ID in ListID.Split(','))
                    {
                        Delete(int.Parse(ID));
                    }
                }
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<Account> GetByPage(int page, int pagesize, List<Account> source)
        {
            List<Account> list = new List<Account>();
            int index = (page - 1) * pagesize;
            int count = source.Count;
            if (pagesize > count)
                list = source;
            else if (pagesize > count - index)
                list = source.GetRange(index, count - index);
            else
                list = source.GetRange(index, pagesize);
            return list;
        }
        public List<Account> GetListBy(DataTable dt)
        {
            List<Account> list = new List<Account>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Account obj = GetBy(dr);
                    list.Add(obj);
                }
            }
            return list;
        }
        public DataTable GetListAll()
        {
            try
            {
                DataTable dt = DB.SelectDataTable(table);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable GetListAllNotAdmin()
        {
            try
            {
                string query = "select * from " + table + " where username <> 'admin'";
                DataTable dt = DB.ExecuteDataTable(query, null);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public Account GetBy(DataRow dr)
        {
            try
            {
                Account obj = new Account();
                obj.Id = (dr[Account.COLUMN_ID] is DBNull) ? 0 : int.Parse(dr[Account.COLUMN_ID].ToString());
                obj.Username = (dr[Account.COLUMN_USERNAME] is DBNull) ? string.Empty : dr[Account.COLUMN_USERNAME].ToString();
                obj.Password = (dr[Account.COLUMN_PASSWORD] is DBNull) ? string.Empty : dr[Account.COLUMN_PASSWORD].ToString();
                obj.Oldpassword = (dr[Account.COLUMN_OLDPASSWORD] is DBNull) ? string.Empty : dr[Account.COLUMN_OLDPASSWORD].ToString();
                obj.Name = (dr[Account.COLUMN_NAME] is DBNull) ? string.Empty : dr[Account.COLUMN_NAME].ToString();
                obj.Status = (dr[Account.COLUMN_STATUS] is DBNull) ? 0 : int.Parse(dr[Account.COLUMN_STATUS].ToString());
                obj.SyntaxSms = (dr[Account.COLUMN_SYNTAX_SMS] is DBNull) ? string.Empty : dr[Account.COLUMN_SYNTAX_SMS].ToString();
                obj.Serviceid = (dr[Account.COLUMN_SERVICEID] is DBNull) ? string.Empty : dr[Account.COLUMN_SERVICEID].ToString();
                return obj;
            }
            catch (Exception)
            {
            }

            return new Account();
        }
        public Account GetBy(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    return GetBy(dr);
                }
            }
            return new Account();
        }
        public Account GetByID(int Id)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
					new SqlParameter(Account.COLUMN_ID,Id),
																									
                }, null);
                return GetBy(dt);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool ChangePassword(Account obj)
        {
            try
            {
                DB.Update(table, new SqlParameter[] {
					new SqlParameter("password",obj.Password),
                    new SqlParameter("oldpassword",obj.Oldpassword),

                }, new SqlParameter[]{
					new SqlParameter("id",obj.Id),
																									 
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        public Account checklogin(string username, string password)
        {
            DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
                new SqlParameter("username",username),
                new SqlParameter("password",password),
                new SqlParameter("status",1)
            }, null);
            return GetBy(dt);
        }

        public Account getAccount(string username, string password)
        {
            DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
                new SqlParameter("username",username),
                new SqlParameter("password",password),
            }, null);
            return GetBy(dt);
        }
        public DataTable Search(string username)
        {
            if (string.IsNullOrEmpty(username))
                return GetListAll();
            string query = string.Format("select * from " + table + " where username like '%{0}%'", username);
            DataTable dt = DB.ExecuteDataTable(query, null);
            return dt;
        }
    }
}
